﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StatisticsScreen.cs" company="">
//   
// </copyright>
// <summary>
// A screen which shows the statistics.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

/*
 * Author: Juan Mompean Esteban
 * Created: 26-11-2011
 */
namespace ITU_vs_Zombies.Menu.Screens
{

    using ITU_vs_Zombies.UsersInfo;

    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;

    /// <summary>
    /// A screen which shows the statistics.
    /// </summary>
    public class StatisticsScreen : GameScreen
    {
        #region Constants and Fields

        /// <summary>
        /// The header.
        /// </summary>
        private readonly string[] header = { "Name", "Time", "Victories", "Losts", "Score" };

        /// <summary>
        /// The pause action.
        /// </summary>
        private readonly InputAction pauseAction;

        /// <summary>
        /// The statistics.
        /// </summary>
        private readonly Statistics statistics;

        /// <summary>
        /// The background texture.
        /// </summary>
        private Texture2D backgroundTexture;

        /// <summary>
        /// The content.
        /// </summary>
        private ContentManager content;

        /// <summary>
        /// The header font.
        /// </summary>
        private SpriteFont headerFont;

        /// <summary>
        /// The table font.
        /// </summary>
        private SpriteFont tableFont;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="StatisticsScreen"/> class. 
        /// Constructor.
        /// </summary>
        public StatisticsScreen()
        {
            this.statistics = new Statistics();
            this.pauseAction = new InputAction(new[] { Keys.Escape }, true);
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Loads graphics content for this screen. The background texture is quite
        /// big, so we use our own local ContentManager to load it. This allows us
        /// to unload before going from the menus into the game itself, wheras if we
        /// used the shared ContentManager provided by the Game class, the content
        /// would remain loaded forever.
        /// </summary>
        /// <param name="instancePreserved">
        /// The instance Preserved.
        /// </param>
        public override void Activate(bool instancePreserved)
        {
            if (instancePreserved)
            {
                return;
            }

            if (this.content == null)
            {
                this.content = new ContentManager(this.ScreenManager.Game.Services, "Content");
            }

            this.backgroundTexture = this.content.Load<Texture2D>("background");
            this.statistics.ReadFile();
            this.headerFont = this.content.Load<SpriteFont>(@"fonts\Bold");
            this.tableFont = this.content.Load<SpriteFont>(@"fonts\Arial");
        }

        /// <summary>
        /// Draws the background screen.
        /// </summary>
        /// <param name="gameTime">
        /// The game Time.
        /// </param>
        public override void Draw(GameTime gameTime)
        {
            var spriteBatch = this.ScreenManager.SpriteBatch;
            var viewport = this.ScreenManager.GraphicsDevice.Viewport;
            var fullscreen = new Rectangle(0, 0, viewport.Width, viewport.Height);

            spriteBatch.Begin();
            spriteBatch.Draw(this.backgroundTexture, fullscreen, Color.White);
            int positionY = 30, positionX = 60;
            foreach (var s in this.header)
            {
                // Draw Score
                spriteBatch.DrawString(
                    this.headerFont, 
                    s, 
                    new Vector2(positionX, positionY), 
                    Color.Green, 
                    0, 
                    Vector2.Zero, 
                    1, 
                    SpriteEffects.None, 
                    1);
                positionX += 130;
            }

            positionX = 30;
            positionY = 70;
            var statsString = new string[6];
            var position = 1;
            foreach (var stats in this.statistics.HighScores)
            {
                statsString[0] = position + "º";
                position++;
                statsString[1] = stats.Name;
                statsString[2] = (stats.Time / 60).ToString();
                statsString[3] = stats.Victories.ToString();
                statsString[4] = stats.Lost.ToString();
                statsString[5] = stats.Score[0].ToString();
                for (var i = 0; i < 6; i++)
                {
                    // Draw Score
                    spriteBatch.DrawString(
                        this.tableFont, 
                        statsString[i], 
                        new Vector2(positionX, positionY), 
                        Color.DarkBlue, 
                        0, 
                        Vector2.Zero, 
                        1, 
                        SpriteEffects.None, 
                        1);

                    positionX += (i == 0) ? 30 : 130;
                }

                positionY += 25;
                positionX = 30;
            }

            spriteBatch.End();
        }

        /// <summary>
        /// The handle input.
        /// </summary>
        /// <param name="gameTime">
        /// The game time.
        /// </param>
        /// <param name="input">
        /// The input.
        /// </param>
        public override void HandleInput(GameTime gameTime, InputState input)
        {
            if (this.pauseAction.Evaluate(input))
            {
                this.ExitScreen();
            }
        }

        /// <summary>
        /// Unloads graphics content for this screen.
        /// </summary>
        public override void Unload()
        {
            if (this.backgroundTexture != null)
            {
                this.backgroundTexture.Dispose();
            }
            if (this.content != null)
            {
                this.content.Unload();
            }
        }

        #endregion
    }
}